package sa.com.sure.ejtimaa.database;


import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.model.AttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingMemberAttachmentInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class MeetingMembersAttachments {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "MeetingMembersAttachments";
	
    public static final String MeetingMemberAttachmentIDField = "MeetingMemberAttachmentID"; 
    public static final String MeetingIDField = "MeetingID";
    public static final String AttachmentIDField = "AttachmentID";
    public static final String MemberIDField = "MemberID";
    public static final String DescriptionField = "Description";
    
    private String[] allColumns = { MeetingMemberAttachmentIDField, MeetingIDField,AttachmentIDField,MemberIDField,DescriptionField};
    
    Cursor cursor = null;
    
    public MeetingMembersAttachments(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static MeetingMembersAttachments Instance = new MeetingMembersAttachments(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
		
	
	public MeetingMemberAttachmentInfo GetItem(int MeetingMemberAttachmentID) throws SQLException
	{	
		try	
		{
			open();
		
			MeetingMemberAttachmentInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingMemberAttachmentIDField+ "=" + MeetingMemberAttachmentID ,null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		item =  getItemInfo(cursor);	     
    	        
        	return 	item;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
		
	
	public void addItem(MeetingMemberAttachmentInfo item) 
	{ 
		try	
		{        
			open();
			
	        ContentValues values = new ContentValues(); 
	        	         
	        values.put(MeetingIDField, item.GetMeetingID());
	        values.put(AttachmentIDField, item.GetAttachmentID());
	        values.put(MemberIDField, item.GetMemberID());
	        values.put(DescriptionField, item.GetDescription());
	        
	        dbHelper.db.insert(TABLE_Name, null, values);
	        
	        Attachments.Instance.addItem(item);
        
		}
	    finally{        	
	        close();
	    }  
                
    } 
	
	
    private MeetingMemberAttachmentInfo getItemInfo(Cursor Description) {  
    	
    	MeetingMemberAttachmentInfo itemInfo = new MeetingMemberAttachmentInfo();
    	    	    
    	itemInfo.SetMeetingID(cursor.getInt(cursor.getColumnIndex(MeetingIDField)));
    	itemInfo.SetAttachmentID(cursor.getString(cursor.getColumnIndex(AttachmentIDField)));    	
    	itemInfo.SetMemberID(cursor.getString(cursor.getColumnIndex(MemberIDField)));
    	itemInfo.SetDescription(cursor.getString(cursor.getColumnIndex(AttachmentIDField)));
    	
    	AttachmentInfo attachment = Attachments.Instance.GetItem(itemInfo.GetAttachmentID());
    	
    	//itemInfo = (MeetingAttachmentInfo) attachment;
    	
    	itemInfo.SetFileName(attachment.GetFileName());
    	itemInfo.SetFileSize(attachment.GetFileSize());
    	itemInfo.SetFileType(attachment.GetFileType());    
    	itemInfo.SetFileContent(attachment.GetFileContent());    	
    	
		return itemInfo;
	}
}
